from django.views.generic import View
from django.http import HttpResponse


class IndexView(View):
    def get(self, request, *args, **kwargs):
        # 1.设置session信息
        # request.session[key] = value
        request.session['username'] = 'zhiliao'
        # request.session['user_id'] = 11000111

        # 2.get用来从session中获取指定值
        # request.session.get(key)
        # username = request.session.get('username')
        # print(username)

        # 3.pop从session中删除一个值
        # request.session.pop('username')
        # username = request.session.get('username')
        # print(username)

        # 4.keys：从session中获取所有的键
        # request.session.keys()
        # keys = request.session.keys()
        # print(keys)

        # 5.items：从session中获取所有的键值对
        # request.session.items()
        # items = request.session.items()
        # print(items)

        # 6.clear：清除当前这个用户的session数据
        # request.session.clear()
        # items = request.session.items()
        # print(items)

        # 7.flush：删除session并且删除在浏览器中存储的session_id，一般在注销的时候用得比较多
        # request.session.flush()
        # username = request.session.get('username')
        # print(username)
        # request.session.flush()
        # username = request.session.get('username')
        # print(username)

        # 8.set_expiry(value)：设置过期时间
        # TODO: 设置120m， 2分钟后过期
        # request.session.set_expiry(120)
        # TODO: 浏览器关闭，session就会过期
        # request.session.set_expiry(0)
        # TODO: None：会使用全局的session配置。在settings.py中可以设置SESSION_COOKIE_AGE来配置全局的过期时间。
        #  默认是1209600秒，也就是2周的时间。
        # request.session.set_expiry(None)

        # 9.clear_expired：清除过期的session
        # 执行后终端再执行：python manage.py clearsessions，即可清除过期的session
        request.session.set_expiry(-1)  # TODO: 设置立即失效session
        request.session.clear_expired()
        return HttpResponse('首页')
